﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LibHuman;

namespace Human
{
    public partial class frm_dmchibo : Form
    {
        private AccessData mDB = new AccessData();
        Language lan = new Language();
        DataSet ds_chibo = new DataSet();
        DataSet ds = new DataSet();
        int flag = 0;
        public frm_dmchibo()
        {
            InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
        }
        private string get_maxid()
        {
            int m_tam;
            ds = mDB.get_data("SELECT Max(to_number(ma,999)) FROM " + mDB.user+ ".dmchibo");
            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                m_tam = 1;
            }
            else
            {
                m_tam = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                m_tam += 1;
            }
            return m_tam.ToString().PadLeft(3, '0').ToString();
        }
        private void btnluu_Click(object sender, EventArgs e)
        {
            string ten_cb="",ma_cb="",nhom="";
            decimal stt=0,m_id;
            if (txt_stt.Text != "")
            {
                stt = decimal.Parse(txt_stt.Text);
            }
            else
            {
                stt = 0;
            }
           
            ten_cb = txt_tencb.Text.Trim();
            if (flag == 0)
            {
                m_id = decimal.Parse(get_maxid());
                txt_macb.Text = m_id.ToString().PadLeft(3, '0');
            }
            else
            {
                if (txt_macb.Text != "")
                    m_id = decimal.Parse(txt_macb.Text.ToString());
            }
            ma_cb = txt_macb.Text;
            if (cbo_nhom.SelectedIndex != -1)
                nhom =cbo_nhom.SelectedValue.ToString().Trim();
            if (txt_tencb.Text == "")
            {
                MessageBox.Show(lan.Change_language_MessageText("Nhập tên!"), AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txt_tencb.Focus();
                return;
            }
            if (!mDB.upd_dmchibo(ma_cb,ten_cb,nhom,stt,mDB.user))
            {
                MessageBox.Show(lan.Change_language_MessageText("Không cập nhật được thông tin!"), AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Information);
                btn_boqua.Focus();
                return;
            }
            flag = 0;
            load_dmchibo();
            enable(false);
            btnluu.Enabled = false;
            btn_boqua.Enabled = false;
            btnsua.Enabled = false;
            btnthem.Enabled = true;
            btnthem.Focus();
        }
        private void load_dmchibo()
        {
            string sql = "";
            sql= "select ma,ten,stt ";
            sql += " ,case nhom ";
            sql += " when 'D' then 'Đảng'";
            sql += " when 'C' then 'Công đoàn'";
            sql += " when 'T' then 'Đoàn thể'";
            sql += " else 'Khác' end as nhom";
            sql+=" from "+mDB.user+".dmchibo";
            sql+=" order by stt";
            ds_chibo = mDB.get_data(sql);
            dgrv_dmchibo.DataSource = ds_chibo.Tables[0];
        }
        private void enable(bool ena)
        {
            cbo_nhom.Enabled = ena;
            txt_tencb.Enabled = ena;
            txt_stt.Enabled = ena;
            btnluu.Enabled = ena;
        }
        private void btnthem_Click(object sender, EventArgs e)
        {
            txt_macb.Text = get_maxid();
            txt_tencb.Text = "";
            txt_stt.Text = "";
            enable(true);
            txt_tencb.Focus();
            btn_boqua.Enabled = true;
            flag = 0;
        }

        private void txt_macb_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void txt_tencb_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}{f4}");
        }

        private void txt_stt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void frm_dmchibo_Load(object sender, EventArgs e)
        {
            load_dmchibo();
            load_cbonhom();
            btnsua.Enabled = false;
            btn_boqua.Enabled = false;
            btnxoa.Enabled = false;
           
            btnthem.Enabled = true;
            btnthem.Focus();
        }
        private void load_cbonhom()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("manhom", typeof(string));
            dt.Columns.Add("tennhom", typeof(string));
            DataRow nrow;
            for (int i = 0; i < 4; i++)
            {
                nrow = dt.NewRow();
                if (i == 0)
                {
                    nrow["manhom"] = "D";
                    nrow["tennhom"] = "Đảng";
                }
                else
                    if (i == 1)
                    {
                        nrow["manhom"] = "C";
                        nrow["tennhom"] = "Công đoàn";
                    }
                    else
                        if (i == 2)
                        {
                            nrow["manhom"] = "T";
                            nrow["tennhom"] = "Đoàn thể";
                        }
                        else
                            if (i == 3)
                            {
                                nrow["manhom"] = "K";
                                nrow["tennhom"] = "Khác";
                            }
                           
                dt.Rows.Add(nrow);
            }
            cbo_nhom.ValueMember = "manhom";
            cbo_nhom.DisplayMember = "tennhom";
            cbo_nhom.DataSource = dt;
            cbo_nhom.SelectedIndex = -1;
        }
        private void btnketthuc_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_boqua_Click(object sender, EventArgs e)
        {
            enable(false);
          
            btnsua.Enabled = false;
            btnthem.Enabled = true;
            btn_boqua.Enabled = false;
            btnxoa.Enabled = false;
        }
        private void btnsua_Click(object sender, EventArgs e)
        {
            enable(true);
            btnthem.Enabled = false;
            btnxoa.Enabled = false;
            btn_boqua.Enabled = true;
            flag = 1;
        }

        private void dgrv_dmchibo_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {

                DataRowView drv = (DataRowView)dgrv_dmchibo.CurrentRow.DataBoundItem;
                txt_macb.Text = drv["ma"].ToString();
                txt_tencb.Text = drv["ten"].ToString();
                txt_stt.Text = drv["stt"].ToString();
                cbo_nhom.Text = drv["nhom"].ToString();
                btnsua.Enabled = true;
                btnxoa.Enabled = true;
                btnsua.Focus();
                btn_boqua.Enabled = true;
                btnluu.Enabled = false;
                btnthem.Enabled = false;
              
            }
            catch { };
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show(lan.Change_language_MessageText("Đồng ý huỷ thông tin này?"),AccessData.Msg, MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                mDB.get_data("delete from " + mDB.user + ".dmchibo where ma='" + txt_macb.Text.Trim() + "'");
            }
            load_dmchibo();
            btnthem.Enabled = true;
            btnsua.Enabled = false;
            btnxoa.Enabled = false;
            btn_boqua.Enabled = false;
         
            
        }
       
        private void txt_stt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;

        }

        private void cbo_nhom_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

       
    }
}